package com.example.matetest.slice.home;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import com.example.matetest.ResourceTable;
import com.example.matetest.network.RequestUtil;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.agp.components.*;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;

public class NetWorkAbilitySlice extends AbilitySlice implements Component.ClickedListener {
    private static final String TAG = NetWorkAbilitySlice.class.getSimpleName();
    private static final HiLogLabel logLabel = new HiLogLabel(HiLog.LOG_APP, 0x00001, TAG);

    private Image imageBack;
    private Text title;

    private Button getRequest;
    private Text getRequestText;
    private Button postRequest;
    private Text postRequestText;
    private Button pictureRequest;
    private Image pictureRequestImage;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_network);

        title = (Text) findComponentById(ResourceTable.Id_tv_title);
        imageBack = (Image) findComponentById(ResourceTable.Id_iv_back);
        imageBack.setClickedListener(this);
        title.setText("NETWORK REQUEST");

        getRequest = (Button) findComponentById(ResourceTable.Id_btn_get_request);
        getRequest.setClickedListener(this);
        getRequestText = (Text) findComponentById(ResourceTable.Id_tv_get_request);
        postRequest = (Button) findComponentById(ResourceTable.Id_btn_post_request);
        postRequest.setClickedListener(this);
        postRequestText = (Text) findComponentById(ResourceTable.Id_tv_post_request);
        pictureRequest = (Button) findComponentById(ResourceTable.Id_btn_picture_request);
        pictureRequest.setClickedListener(this);
        pictureRequestImage = (Image) findComponentById(ResourceTable.Id_image_picture_request);
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    @Override
    public void onClick(Component component) {
        if (component == imageBack) {
            terminate();
        }
        if (component == getRequest) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    getRequest();
                }
            }).start();
        }
        if (component == postRequest) {
            new Thread(new Runnable() {
                @Override
                public void run() {
                    postRequest();
                }
            }).start();
        }
        if (component == pictureRequest) {
            pictureRequest();
        }
    }

    /**
     * get请求
     */
    private void getRequest() {
        String requestUrl = "https://sapi.k780.com";
        HashMap params = new HashMap();
        ZSONObject zsonObject = new RequestUtil().get(requestUrl, null);
        HiLog.info(logLabel, "%{public}s", zsonObject);
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                getRequestText.setText(zsonObject.toString());
            }
        });
    }

    /**
     * post请求
     */
    private void postRequest() {
        String requestUrl = "https://api.fangyaozhongyi.com/api/sysArea/sysArea/list";
        HashMap params = new HashMap();
        params.put("regionType", 1);
        params.put("parentID", 0);
        ZSONObject zsonObject = new RequestUtil().post(requestUrl, params);
        HiLog.info(logLabel, "%{public}s", zsonObject);
        getUITaskDispatcher().asyncDispatch(new Runnable() {
            @Override
            public void run() {
                postRequestText.setText(zsonObject.toString());
            }
        });
    }

    /**
     * 图片加载
     */
    private void pictureRequest() {
        String requestUrl = "https://imgs-bucket-wz.oss-cn-shenzhen.aliyuncs.com/Material/userHead/head_1639562386486.jpg";
        //new RoundedCorners(10)圆角 //new CircleCrop()-圆形
        RequestOptions requestOptions = RequestOptions.centerCropTransform().transform(new RoundedCorners(5)).diskCacheStrategy(DiskCacheStrategy.NONE)//不做磁盘缓存
                .skipMemoryCache(true).placeholder(ResourceTable.Media_head).error(ResourceTable.Media_head);//不做内存缓存;
        Glide.with(getContext())
                .load(requestUrl)
                .apply(requestOptions)
                .into(pictureRequestImage);
    }

}